package cn.iocoder.yudao.module.shop.service.accountrecord;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.shop.controller.admin.accountrecord.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.accountrecord.ShopAccountRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.shop.convert.accountrecord.ShopAccountRecordConvert;
import cn.iocoder.yudao.module.shop.dal.mysql.accountrecord.ShopAccountRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 收支记录 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class ShopAccountRecordServiceImpl implements ShopAccountRecordService {
    @Resource
    private ShopAccountRecordMapper accountRecordMapper;

    @Override
    public Long createAccountRecord(ShopAccountRecordCreateReqVO createReqVO) {

        // 插入
        ShopAccountRecordDO accountRecord = ShopAccountRecordConvert.INSTANCE.convert(createReqVO);

        if(accountRecord.getIsFixed()){
            if(createReqVO.getFixedType() == null){
                throw exception(ACCOUNT_RECORD_FIXED_TYPE_ERROR);
            }

            accountRecord.setPayTime(LocalDateTime.now());
            accountRecord.setPicUrl("");
            accountRecord.setComment("");
        }else{
            if(accountRecord.getPayTime() == null){
                throw exception(ACCOUNT_RECORD_PARAM_PAYTIME);
            }

            if(accountRecord.getPicUrl() == null){
                throw exception(ACCOUNT_RECORD_PARAM_PICURL);
            }

            accountRecord.setComment("");
        }

        accountRecordMapper.insert(accountRecord);
        // 返回
        return accountRecord.getId();
    }

    @Override
    public void updateAccountRecord(ShopAccountRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateAccountRecordExists(updateReqVO.getId());
        // 更新
        ShopAccountRecordDO updateObj = ShopAccountRecordConvert.INSTANCE.convert(updateReqVO);
        accountRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteAccountRecord(Long id) {
        // 校验存在
        validateAccountRecordExists(id);
        // 删除
        accountRecordMapper.deleteById(id);
    }

    private void validateAccountRecordExists(Long id) {
        if (accountRecordMapper.selectById(id) == null) {
            throw exception(ACCOUNT_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public ShopAccountRecordDO getAccountRecord(Long id) {
        return accountRecordMapper.selectById(id);
    }

    @Override
    public List<ShopAccountRecordDO> getAccountRecordList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return accountRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ShopAccountRecordDO> getAccountRecordPage(ShopAccountRecordPageReqVO pageReqVO) {
        return accountRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopAccountRecordDO> getAccountRecordList(ShopAccountRecordExportReqVO exportReqVO) {
        return accountRecordMapper.selectList(exportReqVO);
    }

    @Override
    public List<ShopAccountRecordDO> getAccountRecordListByTimeNoFixed(LocalDateTime startTime, LocalDateTime endTime, Long tenantId){
        return accountRecordMapper.selectListByTimeNoFixed(startTime, endTime, tenantId);
    }

    @Override
    public List<ShopAccountRecordDO> getAccountRecordListFixed(Long tenantId){
        return accountRecordMapper.selectListFixed(tenantId);
    }
}
